package top.infopub.mgr.hr.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hr.dao.DeptInfoDao;
import top.infopub.mgr.hr.dao.OrgInfoDao;
import top.infopub.mgr.hr.dao.PostInfoDao;
import top.infopub.mgr.hr.domain.OrgInfoDomain;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.vo.OrgInfoVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.mkt.PreProject;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class OrgInfoServiceImpl extends GenericServiceImpl<OrgInfo, String> implements OrgInfoService {

	@Autowired
	private OrgInfoDao orgInfoDao;

	@Autowired
	private PostInfoDao postInfoDao;

	@Autowired
	private DeptInfoDao deptInfoDao;

	@Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

	@Override
	public GenericDao<OrgInfo, String> getDao() {
		return orgInfoDao;
	}

	@Override
	public Page<OrgInfoDomain> selectPageByDomainWhere(Page<OrgInfoDomain> page, OrgInfoVo orgInfoVo) {

        //处理时间范围
        if (!StringUtils.isEmpty(orgInfoVo.getDates())) {
            String[] splitTime = orgInfoVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            try {
                orgInfoVo.setEndDate(format.parse(splitTime[1]));
                orgInfoVo.setStartDate(format.parse(splitTime[0]));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
	    PageHelper.startPage(page.getPageNum(), page.getPageSize());
	    return (Page<OrgInfoDomain>)orgInfoDao.selectByVoWhere(orgInfoVo);
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrg(OrgInfo orgInfo){
        // 执行新增
        if (StringUtils.isEmpty(orgInfo.getId())) {
            String genCode = StringUtils.EMPTY;
            if("1".equals(orgInfo.getTypes())){//总部公司
                genCode = "hr_org_info_zb";
            }else if("2".equals(orgInfo.getTypes())){//区域公司
                genCode = "hr_org_info_qy";
            }else if("3".equals(orgInfo.getTypes())){//项目公司
                genCode = "hr_org_info_xm";
            }
            String orgCode = codeGeneratorConfigService.generatorCode(genCode);
            orgInfo.setOrgCode(orgCode);
            if(StringUtils.isEmpty(orgInfo.getParentId())){
                orgInfo.setParentId("-1");
            }
            orgInfo.setCompName(orgInfo.getCompName().trim());
            orgInfo.setState(1);// 默认是1=启用
            orgInfo.setAddBy(SessionUserUtils.getSessionUserId());
            orgInfo.setAddTime(DateUtils.getCurrentDateTime());
            orgInfo.setUptBy(SessionUserUtils.getSessionUserId());
            orgInfo.setUptTime(DateUtils.getCurrentDateTime());
            orgInfoDao.insert(orgInfo);

        }else {// 执行修改
            OrgInfo orgInfoUpdate = orgInfoDao.selectByPrimaryKey(orgInfo.getId());
            orgInfo.setCompName(orgInfo.getCompName().trim());
            orgInfo.setState(orgInfoUpdate.getState());
            orgInfo.setOrgCode(orgInfoUpdate.getOrgCode());
            orgInfo.setAddBy(orgInfoUpdate.getAddBy());
            orgInfo.setAddTime(orgInfoUpdate.getAddTime());
            BeanUtils.copyProperties(orgInfo, orgInfoUpdate);
            orgInfoUpdate.setUptTime(DateUtils.getCurrentDateTime());
            orgInfoUpdate.setUptBy(SessionUserUtils.getSessionUserId());
            this.updateByPrimaryKey(orgInfoUpdate);
        }

    }

    @Override
    public OrgInfoDomain selectById(String id) {
        return orgInfoDao.selectById(id);
    }

    @Override
    public boolean selectByCompName(String compName) {
        OrgInfo orgInfo = orgInfoDao.selectByCompName(compName);
        if (null != orgInfo) return true;
        return false;
    }

    @Override
    public boolean selectByCompNameAndId(String compName, String id) {
        OrgInfo orgInfo = orgInfoDao.selectByCompNameAndId(compName, id);
        if (null != orgInfo) return true;
        return false;
    }

    @Override
    public List<OrgInfo> selectVisibleOrgOfSelf(String orgId) {
        return orgInfoDao.selectVisibleOrgOfSelf(orgId);
    }

    @Override
    public List<OrgInfo> selectByTypes(String types) {
        List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
        if(types.equals("2")){
            orgInfos = orgInfoDao.selectByTypes1();
        }else if(types.equals("3")){
            orgInfos = orgInfoDao.selectByTypes2();
        }
        return orgInfos;
    }

    @Override
    public List<OrgInfo> selectByVisibleIds(String visibleOrgIds) {
        return orgInfoDao.selectByVisibleIds(visibleOrgIds);
    }

    @Override
    public PreProject selectProjectById(String id) {
        return orgInfoDao.selectProjectById(id);
    }

    @Override
    public OrgInfo queryByProjectCode(String proId) {
        return orgInfoDao.queryByProjectCode(proId);
    }

    @Override
    public List<OrgInfo> selectByVisibleIdsAndNotArea(String visibleOrgIds) {
        return orgInfoDao.selectByVisibleIdsAndNotArea(visibleOrgIds);
    }

    @Override
    public List<OrgInfo> selectByVisibleIdsAndProCodeIsnOtNull(String visibleOrgIds) {
        return orgInfoDao.selectByVisibleIdsAndProCodeIsnOtNull(visibleOrgIds);
    }

    @Override
    public List<OrgInfo> selectByProCodeIsnOtNull() {
        return orgInfoDao.selectByProCodeIsnOtNull();
    }

}
